;;; C / C++ Programming Language Support
;;==============================================================


;; Change Major-mode Dispaly Name
;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(add-hook 'c-mode-hook (lambda () (setq mode-name "C")))
(add-hook 'c++-mode-hook (lambda () (setq mode-name "C++")))


;; Use clang-format to format code
;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(when (maybe-require-package 'clang-format)
  (require 'clang-format)
  (global-set-key (kbd "C-c i") 'clang-format-buffer)
  (global-set-key (kbd "C-c u") 'clang-format-region)
  ;; set clang format style
  (setq clang-format-style-option "llvm"))


;; Use irony-mode to support auto-complete
;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(unless *is-macOS*
  (when (maybe-require-package 'irony)
    (add-hook 'c-mode-hook 'irony-mode)
    (add-hook 'c++-mode-hook 'irony-mode)
    (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)))

(after-load 'irony
  (diminish 'irony-mode))

;; Windows performance tweaks. Please uncomment to use.
;;++++++++++++++++++++++++++++++
;; (when (boundp 'w32-pipe-read-delay)
;;   (setq w32-pipe-read-delay 0))
;; Set the buffer size to 64K on Windows (from the original 4K)
;; (when (boundp 'w32-pipe-buffer-size)
;;   (setq irony-server-w32-pipe-buffer-size (* 64 1024)))

;; Use company
;;++++++++++++++++++++++++++++++
(when (package-installed-p 'irony)
  (when (maybe-require-package 'company-irony)
    (after-load 'company
      (add-hook 'irony-mode-hook
                (lambda () (mx/local-push-company-backend 'company-irony))))))


;; Reqire and start company-c-headers
;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(when (maybe-require-package 'company-c-headers)
  (after-load 'company
    (add-hook 'c-mode-common-hook
              (lambda () (mx/local-push-company-backend 'company-c-headers)))))


(provide 'mx-c-cpp)
